package com.explorer.ider.bootguide.fragment;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.LocaleList;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.explorer.ider.bootguide.MainActivity;
import com.explorer.ider.bootguide.R;
import com.explorer.ider.bootguide.itembean.LocaleInfo;
import com.explorer.ider.bootguide.utils.LanguageChangeUtils;
import java.util.List;

/**
 * Created by Eric on 2016/9/5.
 */
public class FragmentLanguage extends Fragment implements AdapterView.OnItemClickListener,AdapterView.OnItemSelectedListener{

    private static final String TAG = "FragmentLanguage";
    private View views;
    private ListView mLanguageListView;
    private List<LocaleInfo> mSelectLanguageStatusString;
    private ArrayAdapter<String> mLanguageArrayAdapter;
    private LocaleList mLocaleList;
    private CardView mLockCardView;
    private String[] mLanguageNameArray;
    private boolean languageSettingOk = false;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (languageSettingOk){

        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (views == null){
            views = inflater.inflate(R.layout.fragment_language, container,false);
            initData();
            initView();
        }
        return views;
    }

    private void initData(){
        //mSelectLanguageStatusString = LanguageChangeUtils.getAllAssetLocales(getActivity(),true);
        mSelectLanguageStatusString = LanguageChangeUtils.getSimpleLanguageList(getActivity());
        mLanguageNameArray = new String[mSelectLanguageStatusString.size()];
        for (int i = 0; i < mSelectLanguageStatusString.size(); i++) {
            mLanguageNameArray[i] = mSelectLanguageStatusString.get(i).getLabel();
        }
        mLanguageArrayAdapter = new ArrayAdapter<String>(getActivity(),R.layout.array_adapter_item,mLanguageNameArray);

    }

    private void initView() {
        mLanguageListView = (ListView) views.findViewById(R.id.id_language_list);
        mLockCardView = (CardView) views.findViewById(R.id.id_focus_lock_card_view);
        mLanguageListView.setAdapter(mLanguageArrayAdapter);
        mLanguageListView.setOnItemClickListener(this);
        mLanguageListView.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent();
        intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER1);
        getActivity().sendBroadcast(intent);
        LanguageChangeUtils.updateLanguage(mSelectLanguageStatusString.get(i).getLocale());
        startAnimation(view);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        startAnimation(view);

    }


    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    public void startAnimation(View v){
        final ValueAnimator animator = ValueAnimator.ofInt((int)mLockCardView.getY(),(int)v.getY());
        animator.setDuration(200);
        animator.setInterpolator(new LinearInterpolator());//线性效果变化
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Integer integer = (Integer) animator.getAnimatedValue();
                mLockCardView.setY(integer);
            }
        });
        animator.start();
    }


    @Override
    public void onPause() {
        super.onPause();
    }

}
